//
//  MYHSelectView.m
//  XunBao
//
//  Created by naonaodawang on 17/6/6.
//  Copyright © 2017年 myh. All rights reserved.
//

#import "MYHSelectView.h"

#define titleWidth ScreenWidth/_titleArray.count
#define titleHeight 40

@interface MYHSelectView ()<UIScrollViewDelegate>
{
    UIViewController *_viewController;
    
    NSInteger currentIndex;
    UIView * navView;
}

//滑块view
@property (nonatomic, strong) UIView *sliderView;
//选择button
@property (nonatomic, strong) UIButton *selectButton;



@property (nonatomic, strong) NSMutableArray *buttonArray;

@end

@implementation MYHSelectView

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化数组
    _buttonArray = [NSMutableArray array];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changOffset:) name:@"Offset" object:nil];
}

#pragma mark -- 根据导航初始化btn
- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    [self initWithTitleButton];
}

#pragma mark -- 根据controller初始化视图
- (void)setControllerArray:(NSArray *)controllerArray
{
    _controllerArray = controllerArray;
    [self initWithController];
}

- (void)initWithTitleButton
{
    navView = [[UIView alloc]initWithFrame:CGRectMake( 0, 0, SCREENWIDTH, 46)];
    [self.view addSubview:navView];
    navView.backgroundColor = [UIColor whiteColor];
    
    self.sliderView = [[UIView alloc]init];
    [navView addSubview:self.sliderView];
    self.sliderView.backgroundColor = BtnBgColor;;
    
    CGFloat btnWidth = (navView.width)/2;
    for (int i = 0; i < _titleArray.count; i++)
    {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [navView addSubview:titleButton];
        titleButton.frame = CGRectMake(btnWidth*i, 0, btnWidth, 44);
        titleButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [titleButton setTitle:_titleArray[i] forState:UIControlStateNormal];
        [titleButton setTitleColor:RGBACOLOR(153, 153, 153, 1) forState:UIControlStateNormal];
        titleButton.tag = 100 + i;
        [titleButton addTarget:self action:@selector(scrollViewSelectToIndex:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0)
        {
            self.selectButton = titleButton;
            //改变选中button的字体颜色
            [self.selectButton setTitleColor:BlackTextColor forState:UIControlStateNormal];
            self.sliderView.center = CGPointMake(titleButton.centerX, titleButton.centerY+23);
            self.sliderView.bounds = CGRectMake(0, 0, 48, 2);
        }
        
        [_buttonArray addObject:titleButton];
    }
    
    //滑块
    
}

#pragma mark - 标题button的点击事件
- (void)scrollViewSelectToIndex:(UIButton *)button
{
    if (self.myBtnIndexBlock)
    {
        self.myBtnIndexBlock(button.tag-100);
    }
    
    [self selectButton:button.tag-100];
    [UIView animateWithDuration:0.3 animations:^{
        self.myScrollView.contentOffset = CGPointMake(SCREENWIDTH*(button.tag-100), 0);
    }];
}

//选择某个标题
- (void)selectButton:(NSInteger)index
{
    if (_buttonArray.count > 0)
    {
        [self.selectButton setTitleColor:RGBACOLOR(153, 153, 153, 1) forState:UIControlStateNormal];
        self.selectButton = _buttonArray[index];
        [self.selectButton setTitleColor:BlackTextColor forState:UIControlStateNormal];
        
        __weak MYHSelectView *myself = self;
        [UIView animateWithDuration:0.3 animations:^{
            myself.sliderView.center = CGPointMake(myself.selectButton.centerX, myself.selectButton.centerY+23);
            myself.sliderView.bounds = CGRectMake(0, 0, 48, 2);
        }];
    }
}

//监听滚动事件判断当前拖动到哪一个了
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / SCREENWIDTH;
    [self selectButton:index];
}

- (void)initWithController
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 46, self.view.frame.size.width, self.view.frame.size.height-46)];
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.pagingEnabled = YES;
    scrollView.scrollEnabled = YES;
    scrollView.contentSize = CGSizeMake(SCREENWIDTH*_controllerArray.count, self.view.frame.size.height-46);
    [self.view addSubview:scrollView];
    self.myScrollView = scrollView;
    
    for (int i = 0; i < _controllerArray.count; i++)
    {
        UIView *viewcon = [[UIView alloc] initWithFrame:CGRectMake(scrollView.width*i, 0, scrollView.width, scrollView.height)];
        UIViewController *viewcontroller = _controllerArray[i];
        viewcon = viewcontroller.view;
        viewcon.frame = CGRectMake(scrollView.width*i, 0, scrollView.width, scrollView.height);
        [scrollView addSubview:viewcon];
        //提价子视图控制器
        [self addChildViewController:viewcontroller];
    }
}

@end
